<template>
    <!-- 添加或修改软件包管理对话框 -->
    <el-drawer :title="title" :visible.sync="open" custom-class="kingmon-drawer" @closed="cancel">
      <div class="kingmon-drawer__content">
        <el-form ref="form" :model="form" :rules="rules" label-width="150px">
          <el-form-item label="操作系统" prop="osVersion">
            <el-select v-model="form.osVersion" clearable filterable>
              <el-option :value="item.value" v-for="item in dict.type.kw_system_version">
                {{ item.label }}
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="硬件架构" prop="cpuArch">
            <el-select v-model="form.cpuArch" clearable filterable>
              <el-option :value="item.value" v-for="item in dict.type.kw_system_arch">
                {{ item.label }}
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="版本号" prop="version" style="width: 300px">
            <el-input v-model="form.version" placeholder="请输入版本号" />
          </el-form-item>
          <el-form-item label="软件包" prop="packagePath">
            <file-upload v-model="form.packagePath" :limit="1"
                         :file-size="1024" :file-type="['rpm', 'tar']" :file-name-start="['kingwow','jwagent']"/>
          </el-form-item>
        </el-form>
      </div>
      <div class="kingmon-drawer__footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-drawer>
</template>

<script>
import { addAttachments } from "@/api/attachments/attachments";

export default {
  name: "UploadFile",
  dicts: ['kw_system_version','kw_system_arch'],
  data() {
    return {
      // 弹出层标题
      title: "上传软件包",
      // 是否显示弹出层
      open: false,
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        osVersion: [
          { required: true, message: "请输选择操作系统", trigger: "blur" }
        ],
        cpuArch: [
          { required: true, message: "请输选择硬件架构", trigger: "blur" }
        ],
        version: [
          { required: true, message: "请输入版本号", trigger: "blur" }
        ],
        packagePath: [
          { required: true, message: "请上传软件包", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.open = true;
  },
  methods: {
    // 取消按钮
    cancel() {
      this.open = false;
      this.$emit("closeDrawer")
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateAttachments(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.$emit("closeDrawer")
            });
          } else {
            addAttachments(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.$emit("closeDrawer")
            });
          }
        }
      });
    },
  }
};
</script>
